package com.example.demo.service;

import com.example.demo.bean.Word;
import com.example.demo.dao.WordDao;
import com.example.demo.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jim Chen
 * @date 2021-04-15
 */
@Service
public class WordService {

    @Autowired
    private WordDao wordDao;

    public String importData() {
        wordDao.deleteAll();

        String file = "/home/jim/tmp/jx.xlsx";
        List<Word> wordList = ExcelUtil.read(file);
        int result = wordDao.insertWordList(wordList);
        return result + "";
    }

    public String translate(String word) {
        String result = "";
        String[] englishNames = word.split("`");

        for (String englishName : englishNames) {
            String englishAbbr;

            if (englishName.contains("_")) {
                String resultWithUnderline = "";
                String[] englishNameSplitteds = englishName.split("_");
                for (String englishNameSplit : englishNameSplitteds) {
                    String englishAbbrByEnglishName = findEnglishAbbrByEnglishName(englishNameSplit);
                    if ("".equals(englishAbbrByEnglishName)) {
                        englishAbbrByEnglishName = englishNameSplit;
                    }
                    resultWithUnderline = resultWithUnderline + englishAbbrByEnglishName + "_";
                }
                if (resultWithUnderline.endsWith("_")) {
                    resultWithUnderline = resultWithUnderline.substring(0, resultWithUnderline.length() - 1);
                }
                englishName = resultWithUnderline;
            }

            String abbr = findEnglishAbbrByEnglishName(englishName);
            if ("".equals(abbr)) {
                englishAbbr = englishName;
            } else {
                englishAbbr = abbr;
            }
            result = result + englishAbbr;
        }
        return result;
    }

    public String reverse(String word) {
        String result = "";
        String[] englishAbbrs = word.split("`");

        for (String englishAbbr : englishAbbrs) {
            String englishName;

            if (englishAbbr.contains("_")) {
                String resultWithUnderline = "";
                String[] englishAbbrSplitteds = englishAbbr.split("_");
                for (String englishAbbrSplit : englishAbbrSplitteds) {
                    String englishNameByEnglishAbbr = findEnglishNameByEnglishAbbr(englishAbbrSplit);
                    if ("".equals(englishNameByEnglishAbbr)) {
                        englishNameByEnglishAbbr = englishAbbrSplit;
                    }
                    resultWithUnderline = resultWithUnderline + englishNameByEnglishAbbr + "_";
                }
                if (resultWithUnderline.endsWith("_")) {
                    resultWithUnderline = resultWithUnderline.substring(0, resultWithUnderline.length() - 1);
                }
                englishAbbr = resultWithUnderline;
            }

            String name = findEnglishNameByEnglishAbbr(englishAbbr);
            if ("".equals(name)) {
                englishName = englishAbbr;
            } else {
                englishName = name;
            }
            result = result + englishName;
        }
        return result;
    }


    public List<Word> findAll() {
        return wordDao.findAll();
    }

    /**
     * 根据英文名称查询英文简写
     * @param englishName
     * @return
     */
    public String findEnglishAbbrByEnglishName(String englishName) {
        String result = "";

        List<Word> wordList = wordDao.findEnglishAbbrByEnglishName(englishName);
        for (Word word : wordList) {
            result = result + word.getEnglishAbbr() + "|";
        }
        if (result.endsWith("|")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    /**
     * 根据英文名称查询
     * @param englishName
     * @return
     */
    public List<Word> findEnglishAbbrsByEnglishName(String englishName) {
        return wordDao.findEnglishAbbrByEnglishName(englishName);
    }

    /**
     * 根据英文简写查询英文名称
     * @param englishAbbr
     * @return
     */
    public String findEnglishNameByEnglishAbbr(String englishAbbr) {
        String result = "";

        List<Word> wordList = wordDao.findEnglishNameByEnglishAbbr(englishAbbr);
        for (Word word : wordList) {
            String englishName = word.getEnglishName();
            if (!"".equals(englishName)) {
                result = result + englishName + "|";
            }
        }
        if (result.endsWith("|")) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }

    /**
     * 根据英文简写查询
     * @param englishAbbr
     * @return
     */
    public List<Word> findEnglishNamesByEnglishAbbr(String englishAbbr) {
        return wordDao.findEnglishNameByEnglishAbbr(englishAbbr);
    }

    /**
     * 根据中文名称查询
     * @param chineseName
     * @return
     */
    public List<Word> findEnglishNameByChineseName(String chineseName) {
        ArrayList<Word> wordList = new ArrayList<>();

        String[] chineseNameSplitted = null;
        if (chineseName.contains("，")) {
            chineseNameSplitted = chineseName.split("，");
        } else if (chineseName.contains("/")) {
            chineseNameSplitted = chineseName.split("/");
        } else if (chineseName.contains("、")) {
            chineseNameSplitted = chineseName.split("、");
        } else {
            wordList.addAll(wordDao.findEnglishNameByChineseName(chineseName));
        }

        if (chineseNameSplitted != null && chineseNameSplitted.length > 0) {
            for (String chineseNameSplit : chineseNameSplitted) {
                List<Word> englishNameByChineseNameList = wordDao.findEnglishNameByChineseName(chineseNameSplit);
                wordList.addAll(englishNameByChineseNameList);
            }
        }

        return wordList;
    }

    public Word getById(Integer id){
        return wordDao.getById(id);
    }
}
